package com.jxl.application.controller.controller;


import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jxl.application.controller.convert.SubjectCategoryConvert;
import com.jxl.application.controller.convert.SubjectLabelConvert;
import com.jxl.application.controller.dto.SubjectCategoryDto;
import com.jxl.application.controller.dto.SubjectLabelDto;
import com.jxl.common.entity.Result;
import com.jxl.common.enums.DeletedCodeEnum;
import com.jxl.common.util.ContextUtil;
import com.jxl.domain.entity.SubjectCategoryBO;
import com.jxl.domain.entity.SubjectLabelBO;
import com.jxl.domain.service.SubjectCategoryDomainService;
import com.jxl.infra.basic.entity.SubjectCategory;
import com.jxl.infra.basic.entity.SubjectLabel;
import com.jxl.infra.basic.entity.SubjectMapping;
import com.jxl.infra.basic.service.SubjectLabelService;
import com.jxl.infra.basic.service.SubjectMappingService;
import com.jxl.infra.entity.UserInfo;
import com.jxl.infra.rpc.AuthUserRpc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;


@RequestMapping("/subject/category")
@RestController
@Slf4j
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @Resource
    private AuthUserRpc authUserRpc;

    /**
     * 分类添加
     *
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            if (log.isInfoEnabled()) { //并发问题解决
                log.info("参数是{}", JSON.toJSONString(subjectCategoryDto));
            }
            Preconditions.checkNotNull(subjectCategoryDto.getCategoryName(), "分类名称不能为空");//guava判断值能不能为空
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryConvert.
                    INSTANCE.convertDtoToSubjectCategoryBO(subjectCategoryDto);
            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询大分类
     *
     * @return
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDto>> queryPrimaryCategory() {
        try {
            List<SubjectCategoryBO> queryPrimaryCategory = subjectCategoryDomainService.getQueryPrimaryCategory();
            List<SubjectCategoryDto> subjectCategoryDtos =
                    SubjectCategoryConvert.INSTANCE.convertBOToSubjectCategoryDtoList(queryPrimaryCategory);
            return Result.ok(subjectCategoryDtos);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询二级分类
     *
     * @return
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDto>> queryCategoryByPrimary(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            List<SubjectCategoryBO> queryPrimaryCategory = subjectCategoryDomainService.queryCategoryByPrimary(subjectCategoryDto.getId());
            List<SubjectCategoryDto> subjectCategoryDtos =
                    SubjectCategoryConvert.INSTANCE.convertBOToSubjectCategoryDtoList(queryPrimaryCategory);
            return Result.ok(subjectCategoryDtos);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 更新分类
     *
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            SubjectCategoryBO subjectCategoryBO =
                    SubjectCategoryConvert.INSTANCE.convertDtoToSubjectCategoryBO(subjectCategoryDto);
            Boolean update = subjectCategoryDomainService.update(subjectCategoryBO);
            return Result.ok(update);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 删除分类
     *
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            Boolean update = subjectCategoryDomainService.delete(subjectCategoryDto.getId());
            return Result.ok(update);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询分类及标签一次性
     */
    @PostMapping("/queryCategoryAndLabel")
    public Result<List<SubjectCategoryDto>> queryCategoryAndLabel(@RequestBody SubjectCategoryDto subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryConvert.INSTANCE.
                    convertDtoToSubjectCategoryBO(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategoryAndLabel(subjectCategoryBO);
            //然后再进行转换
            List<SubjectCategoryDto> list = new ArrayList<>();
            subjectCategoryBOList.forEach(cate -> {
                List<SubjectLabelBO> labelBOList = cate.getLabelBOList();
                //进行转换
                SubjectCategoryDto subjectCategoryDto = SubjectCategoryConvert.INSTANCE.
                        convertBOToSubjectCategoryDto(cate);
                List<SubjectLabelDto> subjectLabelDtos = SubjectLabelConvert.INSTANCE.
                        convertBOToSubjectLabelDtoList(labelBOList);
                subjectCategoryDto.setLabelDTOList(subjectLabelDtos);
                list.add(subjectCategoryDto);
            });
            return Result.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }
}
